package com.mgvlife.ddlw.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.mgvlife.ddlw.entity.Admin;
import com.mgvlife.ddlw.entity.Clazz;
import com.mgvlife.ddlw.entity.Schedule;
import com.mgvlife.ddlw.entity.SignInFile;
import com.mgvlife.ddlw.entity.SignInInfo;
import com.mgvlife.ddlw.formbean.AdminForm;
import com.mgvlife.ddlw.service.AdminService;
import com.mgvlife.ddlw.util.HttpSessionUtil;
import com.mgvlife.ddlw.vo.Result;
import com.mgvlife.ddlw.vo.VoAdmin;
import com.xinhuo.dao.DaoSupport;
import com.xinhuo.util.FileUtil;
import com.xinhuo.util.QueryResult;
import com.xinhuo.vo.JsonEasyUI;

@Service("adminService")
public class AdminServiceImpl extends DaoSupport implements AdminService {

	public Result adminLogin(AdminForm dataForm) throws Exception {
		String name = dataForm.getName();
		String password = dataForm.getPassword();
		String whereSQL = "name=? and password=?";
		Object[] whereParams = { name, password };
		Admin admin = this.find(Admin.class, whereSQL, whereParams);
		if (admin == null) {
			return Result.passwordError();
		}
		HttpSessionUtil.putAdmin(admin.getId(), admin.getName());
		return new Result("success", "登陆成功");
	}

	public Result adminAdd(AdminForm dataForm) throws Exception {
		String name = dataForm.getName();
		boolean isAdminExist = this.checkAdminExist(name);
		if (isAdminExist) {
			return new Result("error", "管理员已存在");
		}
		if (dataForm.getPassword() == null || dataForm.getPwdRepeat() == null
				|| !dataForm.getPassword().equals(dataForm.getPwdRepeat())) {
			return new Result("error", "两次输入密码不一致");
		}
		Admin admin = new Admin();
		admin.setId(UUID.randomUUID().toString());
		admin.setName(name);
		admin.setPassword(dataForm.getPassword());
		this.save(admin);
		return Result.success();
	}

	public Result adminUpdate(AdminForm dataForm) throws Exception {
		if (!dataForm.getPassword2().equals(dataForm.getPwdRepeat())) {
			return new Result("error", "新输入密码不一致");
		}
		String amdinId = dataForm.getId();
		Admin admin = null;
		if (amdinId == null || amdinId.trim().equals("")) {
			return new Result("error", "无此管理员");
		} else {
			admin = this.findById(Admin.class, amdinId);
		}
		if (!admin.getPassword().equals(dataForm.getPassword())) {
			return new Result("error", "原密码不正确");
		}
		admin.setPassword(dataForm.getPwdRepeat());
		this.update(admin);
		return Result.success();
	}

	public JsonEasyUI<VoAdmin> getData(AdminForm dataForm) {
		JsonEasyUI<VoAdmin> result = new JsonEasyUI<VoAdmin>();
		QueryResult<Admin> qr = this.getScrollData(Admin.class, null, null,
				dataForm.getSearchTitle(), dataForm.getSearch(),
				dataForm.getPage(), dataForm.getRows(), dataForm.getSort(),
				dataForm.getOrder());
		result.setTotal(qr.getTotalrecord());
		List<VoAdmin> admin_l = new ArrayList<VoAdmin>();
		for (Admin admin : qr.getResultList()) {
			admin_l.add(new VoAdmin(admin));
		}
		result.setRows(admin_l);
		return result;
	}

	public Result adminDelete(AdminForm dataForm) throws Exception {
		String adminId = dataForm.getId();
		if (adminId != null && adminId.length() > 0) {
			String whereSQL = "";
			String[] ids = adminId.split(";");
			boolean isFirst = true;
			for (int i = 0; i < ids.length; i++) {
				if (isFirst) {
					whereSQL = "id in (?";
					isFirst = false;
				} else {
					whereSQL += ", ?";
				}
			}
			whereSQL += ")";
			List<Admin> admins = this.findAll(Admin.class, whereSQL, ids);
			for (Admin admin : admins) {
				String whereSQL2 = "admin=?";
				Object[] whereParams2 = {admin};
				Schedule schedule = this.find(Schedule.class, whereSQL2, whereParams2);
				if (schedule != null) {
					schedule.setAdmin(null);
					this.update(schedule);
				}
				this.delete(admin);
			}

		} else {
			return new Result("error", "管理员不存在");
		}
		return Result.success();
	}

	public boolean checkAdminExist(String adminName) {
		String whereSQL = "name=?";
		Object[] whereParams = { adminName };
		long count = this.getDataCount(Admin.class, whereSQL, whereParams);
		return count > 0;
	}

}
